package com.hpeu_01.day0112.homework;

/**
 * 第四题：建立exception包，建立Bank类，类中有变量double balance表示存款,
 * Bank类的构造方法能增加存款，Bank类中有取款的发方法withDrawal(double dAmount),
 * 当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数，抛
 * 出NagativeFundsException,如new Bank(100),表示存入银行100元，
 * 当用方法withdrawal(150)，withdrawal(-15)时会抛出自定义异常。
 */
public class Bank {
    private double balance; //存款

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public Bank() {
    }

    public Bank(double balance) {
        this.balance = balance;
    }
    public void withDrawal(double a) throws InsufficientFundsException, NagativeFundsException {
        if (this.getBalance()<a){ //判断取款是否大于余额
                throw new InsufficientFundsException("余额不足，请重新输入");
        }
        if (a<0) { // 判断输入的取款金额是否为负数
            throw new NagativeFundsException("不能输入负数，请重新输入");
        }
    }
}
// 自定义InsufficientFundsException异常
class InsufficientFundsException extends Exception{
    //提供序列化id,保证唯一性。设置自动生成uid，然后alt+enter选择第一个就可以生成
    private static final long serialVersionUID = -4313698352477095185L;
    //创建属性，用于接受异常信息
    private String message;

    @Override
    public String getMessage() {
        return message;
    }

    public InsufficientFundsException() {
    }

    public InsufficientFundsException(String message) {
        this.message = message;
    }
}

//自定义NagativeFundsException异常
class NagativeFundsException extends Exception{

    private static final long serialVersionUID = -5979255874657703574L;
    private String massage;

    public String getMassage() {
        return massage;
    }

    public NagativeFundsException(String massage) {
        this.massage = massage;
    }

    public NagativeFundsException() {
    }
}
class BankDemo{
    public static void main(String[] args) {
        Bank bank = new Bank(100);
        try {
            bank.withDrawal(-100);
        } catch (InsufficientFundsException e) {
            System.out.println(e.getMessage());
        } catch (NagativeFundsException e) {
            System.out.println(e.getMassage());
        }
    }
}